<template>
	<view class="video_box">
		<text>我的标题</text>
		<view class="note__title">
			<u--input placeholder="请输入标题" border="bottom" v-model="videoList.video_title" clearable></u--input>
		</view>
		<view class="u-demo-block">
			<text class="u-demo-block__title">添加视频</text>
			<view class="u-demo-block__content">
				<view class="u-page__upload-item">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
						:maxCount="1" accept="video"></u-upload>
				</view>
			</view>
		</view>
		<view class="btn_send">
			<u-button color="#cde6c7" text="发表" @click="sendVideo()"></u-button>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				videoList:{
					video_title:'',
					src:'',
					uid:'',
				},
				fileList1: [],
			}
		},
		mounted() {
			this.videoList.uid = uni.getStorageSync('token')
		},
		methods:{
			// 新增视频
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {	
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
					// this.noteList.images.push({
					// 	src:lists[i].name
					// })
					this.videoList.src = lists[i].name
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://localhost:8081/minioO/addImage', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			sendVideo() {
				uni.request({
					url: 'http://localhost:8081/video/addVideo',
					method: 'POST',
					data: this.videoList,
					header: {
						'Content-Type': 'application/json',
					},
					dataType: 'json',
					success: (res) => {
						if (res.data.state == 200) {
							uni.showToast({
								title: '发表成功',
								duration: 1000,
							});
						} else {
							uni.$u.toast(res.data.message)
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.video_box{
		margin: 30rpx 40rpx;
		.btn_send {
			width: 90%;
			margin: auto;
			position: fixed;
			bottom: 40rpx;
			left: 0;
			right: 0;
			z-index: 1;
		}
	}
</style>
